//
//  PELocalBatteryVC.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/4/8.
//

#import "RHLocalBatteryDetailVC.h"
#import "RHLocalBatteryClusterFullChartVC.h"
#import "RHInputHeaderView.h"
#import "DevChartParamsModel.h"
#import "SYChartTool.h"
#import "DevChartModel.h"
#import "DeviceChartFieldModel.h"
#import "RHLocalBatteryClusterChartCell.h"
#import "RHDevPackModel.h"
#import "RHLocalCellRealDataModel.h"
#import "RHLocalPackVC.h"
#import "RHLocalSettingModel.h"
#import "RHBleCmdManager.h"
#import "RHBleCmdData.h"
#import "CBLEDataParse.h"
#import "JCDataConvert.h"

@interface RHLocalBatteryDetailVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong)UIView *headerView;

@property (nonatomic, strong) NSArray *dataArr;

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) DevChartParamsModel *chartParamsModel;

@property (nonatomic ,strong) QMUILabel *loopLab;

@property (nonatomic ,strong) QMUILabel *sohLab;

@property (nonatomic ,strong) QMUILabel *socLab;

@property (nonatomic ,strong) QMUILabel *volLab;

@property (nonatomic ,strong) QMUILabel *eleLab;

@property (nonatomic ,strong) QMUILabel *tempLab;
//pack数量,没有主控就默认一个
@property (nonatomic ,assign) NSInteger packCount;
//pack索引,循环读取第个pack下的电芯数据
@property (nonatomic ,assign) NSInteger packIndex;
//单体电压数量
@property (nonatomic ,assign) NSInteger cellVolCount;
//单体温度数量,两个单体共用一个温度
@property (nonatomic ,assign) NSInteger cellTempCount;
////读取完电芯单体个数和温度个数
//@property (nonatomic ,assign) BOOL hasReadVolTempCount;
////读取完电芯电压和温度值
//@property (nonatomic ,assign) BOOL hasReadVolTempValue;
//电芯电压数据
@property (nonatomic ,strong) NSMutableArray *volDataArr;
//电芯温度数据
@property (nonatomic ,strong) NSMutableArray *tempDataArr;
@end

@implementation RHLocalBatteryDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = CFTabViewBgColor;
    
    [self createTableViewData];
    [self setupUI];
    [self getDeviceInfo];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

//数据源
-(void)createTableViewData
{
    NSArray *sectionTitles = @[@"电压统计".rh_localized,@"温度统计".rh_localized];
    NSArray *subTitleArr = @[@"单体电池电压实时曲线".rh_localized,@"单体电池温度实时曲线".rh_localized];
    NSArray *unitArr = @[[NSString stringWithFormat:@"%@(mV)",@"电压".rh_localized],[NSString stringWithFormat:@"%@(℃)",@"温度".rh_localized]];
    NSMutableArray *tempArr = [NSMutableArray array];
    [sectionTitles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        RHLocalCellRealDataModel *model = [[RHLocalCellRealDataModel alloc]init];
        model.sectionTitle = sectionTitles[idx];
        model.title = subTitleArr[idx];
        model.unit = unitArr[idx];
        [tempArr addObject:model];
    }];
    self.dataArr = tempArr.copy;
}

//读取命令
-(void)getDeviceInfo
{
    NSArray *cmdArr = @[@{
        @"field":@"Mtr_paramcycleleft",
        @"regAddr":@(4026),
        @"bitWidth":@(16),
        @"cmdType":@(1)
    },@{
        @"field":@"Mtr_SOH",
        @"regAddr":@(15),
        @"bitWidth":@(16),
        @"ratio":@"0.1",
        @"cmdType":@(1)
    },@{
        @"field":@"Mtr_SOC",
        @"regAddr":@(14),
        @"bitWidth":@(16),
        @"ratio":@"0.1",
        @"cmdType":@(1)
    },@{
        @"field":@"Mtr_TV",
        @"regAddr":@(1),
        @"bitWidth":@(32),
        @"ratio":@"0.001",
        @"cmdType":@(1)
    },@{
        @"field":@"Mtr_Cur",
        @"regAddr":@(13),
        @"bitWidth":@(16),
        @"dataType":@"signed",
        @"ratio":@"0.1",
        @"cmdType":@(1)
    },@{
        @"field":@"Mtr_HT",
        @"regAddr":@(7),
        @"bitWidth":@(16),
        @"ratio":@"0.1",
        @"cmdType":@(1)
    },@{
        @"field":@"Mtr_PackCnt",
        @"regAddr":@(4012),
        @"bitWidth":@(16),
        @"cmdType":@(1)
    }];
    NSMutableArray *mtbArr = [NSMutableArray array];
    [cmdArr enumerateObjectsUsingBlock:^(NSDictionary  *_Nonnull dic, NSUInteger idx, BOOL * _Nonnull stop) {
        RHLocalSettingModel *settingModel = [RHLocalSettingModel yy_modelWithJSON:dic];
        [mtbArr addObject:settingModel];
    }];
    self.cmdArray = mtbArr.copy;
//    self.hasReadVolTempCount = NO;
//    self.hasReadVolTempValue = NO;
    
    [[RHBleCmdManager shareMnger] readDeviceInfoFromBle:self.cmdArray];
}



-(void)setupUI
{
    kWeakSelf;
    ADD_MESSAGE(BLEMSG_PHERAL_SENDMSG_ACK,receiveDeviceMsg:);
    ADD_MESSAGE(BLEMSG_PHERAL_SENDMSG_FINISH,sendCmdFinish);
    
    UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,40, 64)];
    [addButton setImage:[UIImage imageNamed:@"device_battery_cell"] forState:UIControlStateNormal];
    [addButton setBackgroundColor:UIColor.clearColor];
    [addButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [addButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [addButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc] initWithCustomView:addButton];
    self.navigationItem.rightBarButtonItem = rightBarBtn;
    
    [[addButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        RHLocalPackVC *vc = [[RHLocalPackVC alloc] init];
       
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
       
    
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_tableView setBackgroundColor:CFTabViewBgColor];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    _tableView.tableHeaderView = self.headerView;
    [_tableView registerClass:[RHLocalBatteryClusterChartCell class] forCellReuseIdentifier:@"RHLocalBatteryClusterChartCell"];

    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(15, 0, 0, 0));
    }];
    
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    if (@available(iOS 15.0, *)) {
        _tableView.sectionHeaderTopPadding = 0;
    }
    
    [_tableView addHeaderRefresh:^{
        [weakSelf getDeviceInfo];
      
    }];
}

-(void)receiveDeviceMsg:(NSNotification *)no
{
    [super receiveDeviceMsg:no];
    if(!self.recieveMsg)return;
    NSDictionary *dic = [no object];
    if(dic.allKeys.count==0)return;
    NSLog(@"------>收到设备信息:%@",dic);
    if ([dic[CMD_CODE] integerValue]>128) {//命令发送失败,返回错误码
        
    }
    [self.cmdArray enumerateObjectsUsingBlock:^(RHLocalSettingModel * _Nonnull obj, NSUInteger index, BOOL * _Nonnull stop) {
        if(obj.regAddr == [dic[CMD_ADDRESS] integerValue]){
            
            NSData *data = dic[CMD_ADDRESS_VALUE];
            NSString *parse_content = [CBLEDataParse parseData:data settingModel:obj];
            NSLog(@"---->%@ = %@",obj.field,parse_content);
           
            
            [self.dataDic setObject:parse_content forKey:obj.field];
            //----->1.读取到pack数量
            if([obj.field isEqualToString:@"Mtr_PackCnt"]){
                self.packCount = [parse_content integerValue] >0 ? [parse_content integerValue] : 1;
                [self.volDataArr removeAllObjects];
                [self.tempDataArr removeAllObjects];
                //----->2.读每个pack下的电压个数和温度个数
                self.packIndex = 0;
                [self readVolAndTempCount];
            }
            
            //3.读取到电芯电压数量
            if([obj.field isEqualToString:@"Pck_CellV_Cnt"]){
                self.cellVolCount = [parse_content integerValue];
            }
            
            //4.读取到温度数量
            if([obj.field isEqualToString:@"Pck_Tmp_Cnt"]){
//                self.hasReadVolTempCount = YES;
                self.cellTempCount = [parse_content integerValue];
                //----->3.读取每个pack下的电压数据和温度数据
                [self readCellVoltageAndTemperature];
            }
            
            //5.读取到电压值
            if([obj.field isEqualToString:@"Pck_CellV_1"]){
               
                for (int i = 0; i < self.cellVolCount; i ++) {
                    NSData *volData = [data subdataWithRange:NSMakeRange(i*2, 2)];
                    NSInteger volValue = [JCDataConvert dataToInt:volData];
                    NSLog(@"---->第%ld个电压值:%ld",i+1,volValue);
                    [self.volDataArr addObject:@(volValue)];
                }
               
            }
            
            //6.读取到温度值
            if([obj.field isEqualToString:@"Pck_Tmp_1"]){
             
                for (int i = 0; i < self.cellTempCount; i ++) {
                    NSData *tempData = [data subdataWithRange:NSMakeRange(i*2, 2)];
                    double value = [JCDataConvert dataToInt:tempData]*[obj.ratio doubleValue];
                    NSNumber *num = [NSNumber numberWithDouble:value];
                    NSLog(@"---->第%ld个温度值:%@",i+1,num);
                    [self.tempDataArr addObject:[num decimalPlacesWithRoundUpWithDigit:1]];
                }
                   
                //计算最高,最低电压和温度位置
                [self caculateMaxMinValue];
                
                //全部pack的数据已经读完
                if(self.packIndex == self.packCount - 1){
                    
                    
                }else{
                    //读下一个pack的数据
                    self.packIndex ++;
                    [self readVolAndTempCount];
                }
            }
            
            
        };
           
    }];
    [self updateMasterData:self.dataDic];
    [self.tableView reloadData];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [QMUITips hideAllTips];
    });
    
}

#pragma mark --------计算最高,最低电压和温度位置
-(void)caculateMaxMinValue
{
    //读取完电压,温度数据,以及最高电压,最低电压,最高,最低位置,到此数据已读取完毕
    //最高电压
    NSInteger pack_hv = [[self.volDataArr valueForKeyPath:@"@max.intValue"] integerValue];
    //最低电压
    NSInteger pack_lv= [[self.volDataArr valueForKeyPath:@"@min.intValue"] integerValue];
    
    //最高电压位置(如果读取的是Master,高8位是pack序号,低8位是电芯序号)
    __block  NSInteger pack_hv_location = 0;
    __block  NSInteger cell_hv_location = 0;
    
    //最低电压位置(高8位是pack序号,低8位是电芯序号)
    __block NSInteger pack_lv_location = 0;
    __block NSInteger cell_lv_location = 0;
    [self.volDataArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if([obj integerValue] == pack_hv){
            pack_hv_location = idx/self.cellVolCount + 1;//从1开始
            cell_hv_location = idx%self.cellVolCount + 1;
            *stop = YES;
        }
    }];
    [self.volDataArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if(([obj integerValue] == pack_lv) && (pack_lv != 0)){
            pack_lv_location = idx/self.cellVolCount + 1;
            cell_lv_location = idx%self.cellVolCount + 1;
            *stop = YES;
        }
    }];
    //压差
    NSInteger vold_dif = pack_hv - pack_lv;
    
    
    //最高温度  精度0.1
    double pack_ht = [[self.tempDataArr valueForKeyPath:@"@max.doubleValue"] doubleValue];
    //最低温度  精度0.1
    double pack_lt= [[self.tempDataArr valueForKeyPath:@"@min.doubleValue"] doubleValue];
    
    //最高温度位置(高8位是pack序号,低8位是电芯序号)
    __block NSInteger pack_ht_location = 0;
    __block NSInteger cell_ht_location = 0;
    
    //最低温度位置(高8位是pack序号,低8位是电芯序号)
    __block NSInteger pack_lt_location = 0;
    __block NSInteger cell_lt_location = 0;
    [self.tempDataArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if(([obj doubleValue] == pack_ht) && (pack_ht != 0)){
            pack_ht_location = idx/self.cellTempCount + 1;//从1开始
            cell_ht_location = idx%self.cellTempCount + 1;
            *stop = YES;
        }
    }];
    [self.tempDataArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if(([obj doubleValue] == pack_lt) && (pack_lt != 0)){
            pack_lt_location = idx/self.cellTempCount + 1;
            cell_lt_location = idx%self.cellTempCount + 1;
            *stop = YES;
        }
    }];
    //温差
    double temp_dif = pack_ht - pack_lt;
    NSString *tempStr = [NSDecimalNumber decimalStringWithDouble:temp_dif];
    
    //电压x轴
    NSMutableArray *xValues_vol = [NSMutableArray array];
    for (int i = 0; i < self.volDataArr.count; i++) {
        [xValues_vol addObject:[NSString stringWithFormat:@"%d",i]];
    }
    //温度x轴
    NSMutableArray *xValues_temp = [NSMutableArray array];
    for (int i = 0; i < self.tempDataArr.count; i++) {
        [xValues_temp addObject:[NSString stringWithFormat:@"%d",i]];
    }
    
    RHLocalCellRealDataModel *voltModel = [self.dataArr firstObject];
//                    voltModel.packArr = arr;
    voltModel.cellCount = self.cellVolCount;//电压个数
    voltModel.yValues = self.volDataArr.copy;
    voltModel.color = @"8956F8";
    voltModel.xValues = xValues_vol;
    voltModel.maxValue = pack_hv;
    voltModel.minValue = pack_lv;
    if(pack_hv == pack_lv){
        voltModel.maxValue = pack_hv + 1;
    }
    voltModel.max_position_pack = pack_hv_location;
    voltModel.max_position_cell = cell_hv_location;
    voltModel.min_position_pack = pack_lv_location;
    voltModel.min_position_cell = cell_lv_location;
    voltModel.maxDifValue  = vold_dif;
    NSString *hv_location = [NSString stringWithFormat:@"%ld_%ld",pack_hv_location,cell_hv_location];
    NSString *lv_location = [NSString stringWithFormat:@"%ld_%ld",pack_lv_location,cell_lv_location];
    voltModel.pointDataArr = @[@(pack_hv),@(pack_lv),@(vold_dif),hv_location,lv_location];
    
    RHLocalCellRealDataModel *tempModel = [self.dataArr lastObject];
//                    tempModel.packArr = arr;
    tempModel.cellCount = self.cellTempCount;//温度个数
    tempModel.yValues = self.tempDataArr.copy;
    tempModel.color = @"FDD015";
    tempModel.xValues = xValues_temp;
    tempModel.maxValue = pack_ht;
    tempModel.minValue = pack_lt;
    if(pack_ht == pack_lt){
        tempModel.maxValue = pack_ht + 0.6;
    }
    tempModel.max_position_pack = pack_ht_location;
    tempModel.max_position_cell = cell_ht_location;
    tempModel.min_position_pack = pack_lt_location;
    tempModel.min_position_cell = cell_lt_location;
    tempModel.maxDifValue  = temp_dif;
    NSString *ht_location = [NSString stringWithFormat:@"%ld_%ld",pack_ht_location,cell_ht_location];
    NSString *lt_location = [NSString stringWithFormat:@"%ld_%ld",pack_lt_location,cell_lt_location];
    tempModel.pointDataArr = @[@(pack_ht),@(pack_lt),tempStr,ht_location,lt_location];
    
    if (self.volDataArr.count == 0) {
        voltModel.yValues = @[@(0)];
        voltModel.xValues = @[@""];
        voltModel.maxValue = 100;
    }
    
    if (self.tempDataArr.count == 0) {
        tempModel.yValues = @[@(0)];
        tempModel.xValues = @[@""];
        tempModel.maxValue = 100;
    }

}

#pragma mark -------- 命令发送完
-(void)sendCmdFinish
{
    if(self.recieveMsg == NO)return;
    NSLog(@"---->读取到的设备信息:%@",self.dataDic);
   
//    //读取完单体个数和温度个数就读具体的单体电压值和温度值
//    if(self.hasReadVolTempCount){
//        [self readCellVoltageAndTemperature];
//    }else{
//        //读取单体个数和温度个数,两个单体共用一个温度
//        [self readVolAndTempCount];
//    }
    
    [self.tableView endHeaderRefresh];
}

#pragma mark ---------  读取单体电压个数和温度个数 -----
-(void)readVolAndTempCount
{
    
//    self.hasReadVolTempValue = NO;
    NSArray *packArr = @[@{
        @"field":@"Pck_CellV_Cnt",
        @"regAddr":@(1000),
        @"bitWidth":@(16),
        @"ratio":@"1",
        @"cmdType":@(101+self.packIndex)
    },@{
        @"field":@"Pck_Tmp_Cnt",
        @"regAddr":@(2000),
        @"bitWidth":@(16),
        @"ratio":@"1",
        @"cmdType":@(101+self.packIndex)
    }];
    NSMutableArray *mtbArr = [NSMutableArray array];
    [packArr enumerateObjectsUsingBlock:^(NSDictionary  *_Nonnull dic, NSUInteger idx, BOOL * _Nonnull stop) {
        RHLocalSettingModel *settingModel = [RHLocalSettingModel yy_modelWithJSON:dic];
        [mtbArr addObject:settingModel];
    }];
    NSLog(@"---->读取单体个数和温度个数")
    self.cmdArray = mtbArr.copy;
    [[RHBleCmdManager shareMnger] readDeviceInfoFromBle:self.cmdArray];
    
    
}

#pragma mark ---------  读取具体的电压和温度  最高,最低位置,压差-----
-(void)readCellVoltageAndTemperature
{
//    if(self.hasReadVolTempValue){
//        NSLog(@"------->读取电压和温度值完毕");
//        return;
//    }
    
    NSArray *volTempArr = @[@{
        @"field":@"Pck_CellV_1",
        @"regAddr":@(1001),
        @"bitWidth":@(16),
        @"ratio":@"1",
        @"cmdType":@(101+self.packIndex)
    },@{
        @"field":@"Pck_Tmp_1",
        @"regAddr":@(2001),
        @"bitWidth":@(16),
        @"ratio":@"0.1",
        @"cmdType":@(101+self.packIndex)
    }];
    /*,@{            //不读取最高最低位置,改成计算,因为有时间差
        @"field":@"Mtr_HV",
        @"regAddr":@(3),
        @"bitWidth":@(16),
        @"cmdType":@(1),
        @"readAddrCount":@(8)
    }*/
    NSMutableArray *mtbArr = [NSMutableArray array];
    [volTempArr enumerateObjectsUsingBlock:^(NSDictionary  *_Nonnull dic, NSUInteger idx, BOOL * _Nonnull stop) {
        RHLocalSettingModel *settingModel = [RHLocalSettingModel yy_modelWithJSON:dic];
        
        if([settingModel.field isEqualToString:@"Pck_CellV_1"]){
            settingModel.readAddrCount = self.cellVolCount;
        }
        if([settingModel.field isEqualToString:@"Pck_Tmp_1"]){
            settingModel.readAddrCount = self.cellTempCount;
        }
        [mtbArr addObject:settingModel];
    }];
    NSLog(@"---->读取单体个数和温度个数")
    self.cmdArray = mtbArr.copy;
    [[RHBleCmdManager shareMnger] readDeviceInfoFromBle:self.cmdArray];
}


#pragma mark ------ 更新顶部数据
-(void)updateMasterData:(NSDictionary*)dic
{
    self.loopLab.text = [NSString stringWithFormat:@"%@%@",ValidNullDataToString(dic[@"Mtr_paramcycleleft"], @"--"),@""];
    self.sohLab.text  = [NSString stringWithFormat:@"%@%@",ValidNullDataToString(dic[@"Mtr_SOH"], @"--"),@""];
    self.socLab.text  = [NSString stringWithFormat:@"%@%@",ValidNullDataToString(dic[@"Mtr_SOC"], @"--"),@""];
    NSNumber *curValue = [NSNumber numberWithFloat:[dic[@"Mtr_TV"] floatValue]];
    self.volLab.text  = [curValue decimalPlacesWithRoundUpWithDigit:1];
    self.eleLab.text = [NSString stringWithFormat:@"%@%@",ValidNullDataToString(dic[@"Mtr_Cur"], @"--"),@""];
    self.tempLab.text = [NSString stringWithFormat:@"%@%@",ValidNullDataToString(dic[@"Mtr_HT"], @"--"),@""];
    
}

#pragma mark ------- action -----
-(void)fullAction:(NSInteger)section
{
    RHLocalBatteryClusterFullChartVC *fullVC = [[RHLocalBatteryClusterFullChartVC alloc]initWithModel:self.dataArr[section] ];
    [self.navigationController pushViewController:fullVC animated:NO];
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 45;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = kView;
    RHLocalCellRealDataModel *model = self.dataArr[section];
    UILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.font = kBOLDSYSTEMFONT(15);
    titleLab.textColor = kColorRGB(0, 0, 0);
    titleLab.textAlignment = NSTextAlignmentLeft;
    titleLab.text = model.sectionTitle;
    [headView addSubview:titleLab];
    
    QMUIButton *fullBtn = [[QMUIButton alloc]init];
    [fullBtn setImage:[UIImage imageNamed:@"arrow"] forState:UIControlStateNormal];
    [headView addSubview:fullBtn];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headView).offset(15);
        make.bottom.mas_offset(-10);
     }];
    
    [fullBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset (-15);
        make.width.height.offset(35);
        make.centerY.equalTo(titleLab);
    }];
    
    kWeakSelf;
    [[fullBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf fullAction:section];
    }];
    
   return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    RHLocalBatteryClusterChartCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RHLocalBatteryClusterChartCell"];
    cell.chartModel = self.dataArr[indexPath.section];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
}


-(UIView *)headerView
{
    if(!_headerView){
        kWeakSelf;
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUI_WINDOW_W, kScaleW(80)*2+15)];
        _headerView.backgroundColor = CFTabViewBgColor;
        NSArray *dataArr = @[@{CellTitle:@"循环次数".rh_localized,
                                  CellContent:@"--",
                                  CellIcon:@"local_cell_loop",
                                  @"color":@"ffffff",
                                  CellUint:@""},
                                @{CellTitle:@"SOH",
                                  CellContent:@"--",
                                  CellIcon:@"local_cell_soh",
                                  @"color":@"ffffff",
                                  CellUint:@"%"},
                                @{CellTitle:@"SOC",
                                  CellContent:@"--",
                                  CellIcon:@"local_cell_soc",
                                  @"color":@"ffffff",
                                  CellUint:@"(%)"},
                                @{CellTitle:@"电压".rh_localized,
                                  CellContent:@"--",
                                  CellIcon:@"local_cell_vol",
                                  @"color":@"ffffff",
                                  CellUint:@"(V)"},
                                @{CellTitle:@"电流".rh_localized,
                                  CellContent:@"--",
                                  CellIcon:@"local_cell_ele",
                                  @"color":@"ffffff",
                                  CellUint:@"(A)"},
                                @{CellTitle:@"温度".rh_localized,
                                  CellContent:@"--",
                                  CellIcon:@"local_cell_temp",
                                  @"color":@"ffffff",
                                  CellUint:@"(­°C)"}
                                ];
        __block UIView *lastView;
        __block CGFloat itemW = (kUI_WINDOW_W-30-20)/3;
        [dataArr enumerateObjectsUsingBlock:^(NSDictionary  *_Nonnull dic, NSUInteger idx, BOOL * _Nonnull stop) {
            UIView *itemView = [weakSelf itemViewWithIndex:idx title:dic[CellTitle] image:dic[CellIcon] unit:dic[CellUint]];
            [_headerView addSubview:itemView];
            if(idx == 0){
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_offset(15);
                    make.top.mas_offset(15);
                    make.width.offset(itemW);
                    make.height.offset(kScaleW(65));
                }];
            }else{
                if(idx%3==0){//换行
                    [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.left.mas_offset(15);
                        make.top.equalTo(lastView.mas_bottom).offset(10);
                        make.size.equalTo(lastView);
                    }];
                }else{
                    [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.left.equalTo(lastView.mas_right).offset(10);
                        make.centerY.equalTo(lastView);
                        make.size.equalTo(lastView);
                    }];
                }
            }
                    
            lastView = itemView;
        }];
        
        [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_offset(0);
        }];
        
        [_headerView setNeedsLayout];
        [_headerView layoutIfNeeded];
        _headerView.height = lastView.bottom+15;
    }
    return _headerView;
}

//功率统计(装机容量,总功率,当前功率,能效)
-(UIView*)itemViewWithIndex:(NSInteger)idx title:(NSString*)title image:(NSString*)imageStr unit:(NSString*)unit
{
    UIView *bgView = kView;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    bgView.backgroundColor = UIColor.whiteColor;
    UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageStr]];
    [bgView addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(25);
        make.left.top.mas_offset(15);
    }];
    
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.textAlignment = NSTextAlignmentLeft;
    titleLab.numberOfLines = 0;
    titleLab.text = [NSString stringWithFormat:@"%@%@",title,unit];
    titleLab.textColor = kColorRGBA(63, 68, 67, 0.7);
    titleLab.font = kSYSTEMFONT(13);
    [bgView addSubview:titleLab];
    
    QMUILabel *contentLab = [[QMUILabel alloc]init];
    contentLab.textAlignment = NSTextAlignmentLeft;
    contentLab.tag = 200 + idx;
    contentLab.text = @"--";
    contentLab.textColor = kColorRGBA(63, 68, 67, 1);
    contentLab.font = [UIFont boldSystemFontOfSize:16];
    [bgView addSubview:contentLab];
    
    [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLab);
        make.right.mas_offset(-8);
        make.bottom.mas_offset(-15);
    }];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(img.mas_right).offset(8);
        make.right.mas_offset(-3);
        make.centerY.equalTo(img);
    }];
    
    if(idx == 0){
        self.loopLab = contentLab;
    }else if (idx == 1){
        self.sohLab = contentLab;
    }else if (idx == 2){
        self.socLab = contentLab;
    }else if (idx == 3){
        self.volLab = contentLab;
    }else if (idx == 4){
        self.eleLab = contentLab;
    }else if (idx == 5){
        self.tempLab = contentLab;
    }
    return bgView;
}



-(NSMutableArray *)volDataArr
{
    if(!_volDataArr){
        _volDataArr = [NSMutableArray array];
    }
    return _volDataArr;
}

-(NSMutableArray *)tempDataArr
{
    if(!_tempDataArr){
        _tempDataArr = [NSMutableArray array];
    }
    return _tempDataArr;
}
@end
